<?php 
namespace Admin\Controller;
class ProductController extends BaseController{
	function brand(){
		$brandOb= M('brand');
		$reCount = $brandOb->count();
		$re = $brandOb->select();
		$this->assign('reCount',$reCount);
		$this->assign('re',$re);
		$this->display();
	}
	function brand_del(){
		$id=$_GET['id'];
		$brandOb = M('brand');
		$re = $brandOb->where("id = $id")
						->delete();
		echo $id;
	}
	function update(){
		$id = $_GET['id'];

	}
	function brand_add(){
		$this->display();
	}
	function lister(){
	
	$apkOb = M('protype');
		//var_dump($arr);
		$productOb = M('product');
		$re=$productOb->select();
		$this->assign('ops',$ops);
		$this->assign('re',$re);
		$this->display();
	}
	function category_ajax(){
	$categoryObj = M('protype');
	$data = $categoryObj->field("id,pId,name")->select();
	echo json_encode($data);
}
	function category(){
		$this->display();
	}
	function category_add(){
		$this->display();
	}
	function xiajia(){
		$id = $_GET['id']; 
		$productOb  = M('product');
		$data = array('status' =>"0", );
		$re = $productOb->where("id = $id")
						->data($data)
						->save();
		if($re){
			echo $id;
		}
	}
	function shangjia(){
		$id = $_GET['id'];
		$productOb=M('product');
		$data = array('status'=>'1');
		$re = $productOb->where("id = $id")
						->data($data)
						->save();
		if($re){
			echo $id;
		}
	}
	function product_del(){
		$id= $_GET['id'];
		$productOb=M('product');
		$re = $productOb->where("id = $id")
						->delete();
		if($re){
			echo $id;
		}
	}
	function product_add(){
		$optionOb=D('Type');
		$optionStr = $optionOb->showOption();
		$this->assign('optionStr',$optionStr);
		$this->display();
	}
		function product_save(){
	
			if($_POST){//保存数据
			$productOb=M('Product');
			$cRe=$productOb->create();
			if($cRe){
				$re=$productOb->add();
				if($re){
					//先保存图片
					$uploadOb=new \Think\Upload();
					$uploadOb->mimes=array('image/jpeg','image/png','image/gif');
					$uploadOb->autoSub=false;
					$uploadOb->rootPath="./Public/Uploads/product/";
					$imageArr=$uploadOb->upload();
					$imageInfo="没有选择图片";
					if($imageArr){
						$productimageOb=new \Think\Model('productimage');
						//实例化图片类
						$imageOb=new \Think\Image();
						foreach($imageArr as $v){
							$imagename=$v['savename'];
							//生成缩略图
							$imageOb->open("./Public/Uploads/product/".$imagename)
									->thumb(130,130,\Think\Image::IMAGE_THUMB_FILLED)
									->save("./Public/Uploads/product/s_/".$imagename);
							$productid=$re;
							//图片保存成功后，把图片名称写入产品图片表
							$imageRe=$productimageOb->data(array('productid'=>$productid,'imagename'=>$imagename))
											->add();
						}
						if($imageRe){
							$imageInfo="图片上传成功";
						}else{
							$imageInfo="图片上传失败";
						}
					}
					$this->success("添加成功，$imageInfo",U("Admin/Product/lister"));
				}else{
					$this->error("添加失败",U('Admin/Product/lister'));
				}
				exit();
			}else{
				$info=$productOb->getError();
				$this->assign('info',$info);
				$this->assign('arr',$_POST);
			}
		}
		$this->display();}
}